package controller

import (
	"strconv"
	"strings"

	"github.com/kataras/iris"
	"github.com/kataras/iris/sessions"

	"im/common"
	"im/errcode"
	"im/model"
	"im/requests"
	"im/service"
)

type Posts struct {
	Base
	Session *sessions.Session
	Ctx     iris.Context
}

// swagger:route GET /posts/recommends posts get-posts-recommends
// 获取推荐的帖子列表
//
// 现在的逻辑是获取user所在的群的所有用户发过的所有的帖子
//   Security:
//   - jwt_key:
//   Responses:
//     200: GetPosts
func (p Posts) GetRecommends() {
	var user = p.Ctx.Values().Get("user").(model.Users)
	var param = requests.GetRecommendPosts{
		Pagination: requests.Pagination{
			Page:  1,
			Limit: 10,
		},
		Filter: 0,
	}
	_ = p.Ctx.ReadQuery(&param)
	if param.Limit > 50 {
		param.Limit = 50
	}
	err := service.Validate.Struct(param)
	if err != nil {
		p.SendCry(err)
		return
	}
	list, err := service.NewPosts().ListRecommend(user, int64(user.AppsId), param)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(list)
}

// swagger:route GET /posts posts get-posts
//   获取帖子列表
//
//   获取帖子列表
//   Security:
//   - jwt_key:
//   Responses:
//     200:GetPosts
func (p Posts) Get() {
	var user = p.Ctx.Values().Get("user").(model.Users)
	var pagination = requests.Pagination{
		Page:  1,
		Limit: 10,
	}
	_ = p.Ctx.ReadQuery(&pagination)
	if pagination.Limit > 50 {
		pagination.Limit = 50
	}
	list, err := service.NewPosts().List(user.Id, pagination)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(list)
}

// swagger:route GET /posts/others/{userId} posts get-posts-others-id
//   获取他人帖子列表,userId为其他人的userId
//
//   获取他人帖子列表
//   Security:
//   - jwt_key:
//   Responses:
//     200:GetPosts
func (p Posts) GetOthersBy(userId int64) {
	var pagination = requests.Pagination{
		Page:  1,
		Limit: 10,
	}
	_ = p.Ctx.ReadQuery(&pagination)
	if pagination.Limit > 50 {
		pagination.Limit = 50
	}
	list, err := service.NewPosts().List(userId, pagination)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(list)
}

// swagger:route DELETE /posts posts delete-posts-by-id
//   删除帖子
//
//   删除帖子
//   Security:
//   - jwt_key:
//   Responses:
//     200: EmptyResponse
func (p Posts) DeleteBy(id int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)
	err := service.NewPosts().DeleteById(user.Id, id)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(nil)
}

// swagger:route GET /posts/{id} posts get-posts-by-id
//   获取帖子详情
//
//   获取帖子详情
//   Security:
//   - jwt_key:
//   Responses:
//     200: GetPosts
func (p Posts) GetBy(id int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)
	m, err := service.NewPosts().GetById(user.Id, id)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(m)
}

// swagger:route GET /posts/{id}/replys posts get-posts-by-id-replys
//   获取帖子评论列表
//
//   获取帖子评论列表
//   Security:
//   - jwt_key:
//   Responses:
//     200: GetPosts
func (p Posts) GetByReplys(id int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)

	var pagination = requests.Pagination{
		Page:  1,
		Limit: 10,
	}
	_ = p.Ctx.ReadQuery(&pagination)
	if pagination.Limit > 50 {
		pagination.Limit = 50
	}
	m, err := service.NewPosts().GetReplysList(user.Id, id, pagination)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(m)
}

// swagger:route GET /posts/{id}/replys/{replyId} posts get-posts-by-id-replys-by-id
//   获取评论详情
//
//   获取评论详情
//   Security:
//   - jwt_key:
//   Responses:
//     200: GetPosts
func (p Posts) GetByReplysBy(id, replyId int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)
	m, err := service.NewReplys().Get(user.Id, replyId)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(m)
}

// swagger:route POST /posts posts post-posts
//   创建帖子
//
//   创建帖子
//   Security:
//   - jwt_key:
//   Responses:
//     200:
func (p Posts) Post() {
	var user = p.Ctx.Values().Get("user").(model.Users)
	mm := requests.PostPosts{
		PostsModifiable: model.PostsModifiable{
			Location: ",",
			Switch:   1,
		},
	}
	_ = p.Ctx.ReadJSON(&mm)
	err := service.Validate.Struct(&mm)
	if err != nil {
		p.SendCry(errcode.RequestParamInvalid)
		return
	}
	var modify = mm.PostsModifiable
	fromGrousIdSlice := strings.Split(mm.FromGroupId, ",")
	for _, row := range fromGrousIdSlice {
		modify.FromGroupId = append(modify.FromGroupId, common.StringToInt64(row))
	}
	_, err = service.NewPosts().Create(user.Id, modify)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(nil)
}

// swagger:route POST /posts/{id}/like posts post-posts-like-by-id
//   点赞帖子
//
//   点赞帖子,如果本来就有赞,静默失败
//   Security:
//   - jwt_key:
//   Responses:
//     200:EmptyResponse
func (p Posts) PostByLike(id int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)
	err := service.NewPosts().Like(user.Id, id)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(nil)
}

// swagger:route POST /posts/{id}/forward posts post-posts-forward-by-id
//   转发帖子
//
//   转发帖子的时候要通知服务端, 记录一下帖子所在的群
//   Security:
//   - jwt_key:
//   Responses:
//     200:EmptyResponse
func (p Posts) PostByForward(id int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)
	var param = requests.PostPostsForward{}
	_ = p.Ctx.ReadJSON(&param)
	newGroups := strings.Split(param.GroupIds, ",")
	if len(newGroups) == 0 {
		p.SendCry(errcode.RequestParamInvalid)
		return
	}
	m, err := service.NewPosts().GetById(user.Id, id)
	if err != nil {
		p.SendCry(err)
		return
	}
	var allGroups = m.FromGroupId
	if allGroups == nil {
		allGroups = []int64{}
	}
	for _, g := range newGroups {
		gg, err := strconv.ParseInt(g, 10, 64)
		if err != nil {
			continue
		}
		allGroups = append(allGroups, gg)
	}
	err = service.NewPosts().UpdateById(user.Id, id, model.PostsModifiable{
		FromGroupId: allGroups,
	})
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(nil)
}

// swagger:route POST /posts/{id}/unlike posts post-posts-unlike-by-id
//   取消点赞帖子
//
//   取消点赞帖子,如果本来就没有赞,静默失败
//   Security:
//   - jwt_key:
//   Responses:
//     200:EmptyResponse
func (p Posts) PostByUnlike(id int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)
	err := service.NewPosts().Unlike(user.Id, id)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(nil)
}

// swagger:route POST /posts/{id}/replys posts post-posts-by-replys
//   评论某个帖子
//
//   评论某个帖子
//   Security:
//   - jwt_key:
//   Responses:
//     200: EmptyResponse
func (p Posts) PostByReplys(id int64) {
	var (
		param = requests.PostReplys{}
		user  = p.Ctx.Values().Get("user").(model.Users)
	)
	_ = p.Ctx.ReadJSON(&param)
	err := service.NewReplys().Create(user.Id, id, 0, param)
	if err != nil {
		p.SendCry(err)
		return
	}
	err = service.NewInternalMails().AddCommentMails(id, param.Title, user)
	if err != nil {
		p.SendCry(err)
		return
	}

	p.SendSmile(nil)
}

// swagger:route GET /posts/{id}/replys/{replyId}/replys posts get-posts-by-replys-by-replys
// 获取某个帖子下面某条评论的子评论列表
//
// 获取某个帖子下面某条评论的子评论列表
//   Security:
//   - jwt_key:
//   Responses:
//     200: GetReplysByIdReplys
func (p Posts) GetByReplysByReplys(id int64, replyId int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)

	var pagination = requests.Pagination{
		Page:  1,
		Limit: 10,
	}
	_ = p.Ctx.ReadQuery(&pagination)
	if pagination.Limit > 50 {
		pagination.Limit = 50
	}
	res, err := service.NewReplys().GetSubReplyList(user.Id, replyId, pagination)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(res)
}

// swagger:route POST /posts/{id}/replys/{replyId}/replys posts post-posts-by-replys-by-replys
//   对某个评论做回复
//
//   对某个评论做回复
//   Security:
//   - jwt_key:
//   Responses: EmptyResponse
//     200:
func (p Posts) PostByReplysByReplys(id int64, replyId int64) {
	var (
		params = requests.PostReplys{}
		user   = p.Ctx.Values().Get("user").(model.Users)
	)
	_ = p.Ctx.ReadJSON(&params)
	err := service.NewReplys().Create(user.Id, id, replyId, params)
	if err != nil {
		p.SendCry(err)
		return
	}
	err = service.NewInternalMails().AddReplyMails(id, replyId, params.Title, user)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(nil)
}

// swagger:route POST /posts/{id}/replys/{replyId}/like posts post-posts-by-replys-by-like
//   点赞某个回复
//
// 点赞某个回复
//   Security:
//   - jwt_key:
//   Responses:
//     200: EmptyResponse
func (p Posts) PostByReplysByLike(id int64, replyId int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)
	err := service.NewReplys().Like(user.Id, id, replyId)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(nil)
}

// swagger:route POST /posts/{id}/replys/{replyId}/unlike posts post-posts-by-replys-by-unlike
// 取消某个回复的点赞
//
// 取消某个回复的点赞
//   Security:
//   - jwt_key:
//   Responses:
//     200: EmptyResponse
func (p Posts) PostByReplysByUnlike(id int64, replyId int64) {
	var user = p.Ctx.Values().Get("user").(model.Users)
	err := service.NewReplys().Unlike(user.Id, id, replyId)
	if err != nil {
		p.SendCry(err)
		return
	}
	p.SendSmile(nil)
}
